package com.paireach.bw.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.adapter.ExceptionListAdapter;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.ExceptionInfoBean;
import com.paireach.bw.bean.ExceptionListBean;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2017/5/31.
 *
 */

public class WarnListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    private TextView layout_top_change;
    private ImageView layout_top_back;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private List<ExceptionInfoBean> exceptionList = new ArrayList<>();
    private ExceptionListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Transition bottom = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_bottom);
            getWindow().setEnterTransition(bottom);
        }
        setContentView(R.layout.activity_exception_list);
        showProgressDialog();
        new WayBillRequest().getExceptionList(phoneNumber);

        findViews();
        initViews();
    }

    private void initViews() {
        layout_top_change.setText("异常列表");
        layout_top_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        swipeRefreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ExceptionListAdapter(exceptionList);
        recyclerView.setAdapter(adapter);
    }

    private void findViews() {
        layout_top_change = (TextView) findViewById(R.id.layout_top_change);
        layout_top_back = (ImageView) findViewById(R.id.layout_top_back);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        swipeRefreshLayout.setRefreshing(false);
        if (event.getRequestTag() == RequestTag.EXCEPTION_LIST) {
            ExceptionListBean bean = GsonUtil.jsonToBean(event.getJson(), ExceptionListBean.class);
            if ("1".equals(bean.getStatus())) {
                exceptionList.clear();
                exceptionList.addAll(bean.getOrders());
                adapter.notifyDataSetChanged();
            } else {
                showToast("没有数据");
            }
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        dismissProgressDialog();
    }

    @Override
    public void onRefresh() {
        new WayBillRequest().getExceptionList(phoneNumber);
    }
}
